package vtca.nmcuong.btlist;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class PAdapter extends ArrayAdapter<Point> {


	private List<Point> items;
	
	private Handler del;
	private Handler setdf;
	private Handler getoldDF;
	
	public int oldDf;
	
	public List<Point> getItems() {
		return items;
	}

	
	public PAdapter(Context context, int textViewResourceId,
			List<Point> items, Handler del, Handler setdf, Handler getoldDF) {
		//		super(context, textViewResourceId, items);
		super(context, textViewResourceId, items);
		this.items = items;
		this.del = del;
		this.setdf = setdf;
		this.getoldDF = getoldDF;
	}


	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		if (v == null) {
			LayoutInflater infalInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			v = infalInflater.inflate(R.layout.point_item, null);
		}
		
		final Point point = items.get(position); //gia tri cua object ben trong
		
		if (point != null) {
			TextView toadoX = (TextView) v.findViewById(R.id.tdX); //display X
			toadoX.setText("" + point.getTdX());

			TextView toadoY = (TextView) v.findViewById(R.id.tdY); //displayY
			toadoY.setText("" + point.getTdY());

			final TextView pointName = (TextView) v.findViewById(R.id.pointName);
			pointName.setText("" + point.getpName());


			//			pointName.setOnClickListener(new View.OnClickListener() {
			//				public void onClick(View v) {
			//					Toast.makeText(getContext(), point.getpName(), Toast.LENGTH_LONG).show();
			//				}
			//			});
			//
			
	
			
			
			if(point.isDf==true){
				
				oldDf= items.indexOf(point);
				getOldDefaulf();
				
//				final TextView pointName2 = (TextView) v.findViewById(R.id.pointName);
				pointName.setText("" + point.getpName() + " (Default point)");

				View dflayout = v.findViewById(R.id.lyDefault);
				dflayout.setBackgroundColor(Color.DKGRAY);

				ImageButton delete = (ImageButton)v.findViewById(R.id.btnDelete);
				delete.setBackgroundResource(R.drawable.delete002);
				delete.setClickable(false);





			}
			else{

				ImageButton delete = (ImageButton)v.findViewById(R.id.btnDelete);
				//				delete.setBackgroundColor(Color.RED);
				delete.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						callRemove(point, items);
					}
				});

				View oclSetDf = v.findViewById(R.id.lyDefault); //oclSetDf OnLongClick de set default
				oclSetDf.setOnLongClickListener(new View.OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						// TODO Auto-generated method stub

						setDefaulf(point, items);
						return false;


					}

				});
				
				
				

			}
		}
		return v;
	}


	private void callRemove(final Point point, final List<Point> items) {
		Message msg = new Message();
		int position = items.indexOf(point);
		msg.arg1 = position;
		del.sendMessage(msg); // day ra ngoai, y/c papa thuc hien lenh

	}


	private void setDefaulf(final Point point, final List<Point> items) {
		Message msg = new Message();
		int position = items.indexOf(point);
		msg.arg1 = position;
		msg.arg2 = oldDf;
		setdf.sendMessage(msg);

	}
	
	
	private void getOldDefaulf(){
		Message msg = new Message();
		msg.arg1 = oldDf;
		getoldDF.sendMessage(msg);
	}



}
